package leetcode.t2String;

/**
 * @author K
 * @date 2024/1/23 14:34
 */
public class T8_MyAtoi {

    public static int myAtoi1(String s) {
        char[] chars = s.trim().toCharArray();
        if (chars.length < 1) return 0;

        //判断正负数、循环起始位置
        int sign = 1, i = 1;
        int broder = Integer.MAX_VALUE / 10;
        if (chars[0] == '-')
            sign = -1;
        else if (chars[0] != '+')
            i = 0;

        int result = 0;
        for (int j = i; j < chars.length; j++) {
            if (chars[j] < '0' || chars[j] > '9') break;
            if (result > broder || result == broder && chars[j] > '7') {
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }

            result = result * 10 + chars[j] - '0';
        }

        return sign * result;
    }

    public static void main(String[] args) {
        int i = myAtoi1("    -14");
        System.out.println();
    }

}
